package control;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Kipmol;
import validation.ValidateForm;
import database.DBClass;
import java.util.List;

/**
 *
 * @author Jagadeesh
 */
public class ControllerServlet extends HttpServlet {

    //declare values to get form values from jsp page
    String namaPemohon;
    String NIKPemohon;
    String NoKK;
    String AlmtPemohon;
    String RTPemohon;
    String RWPemohon;
    String KodePosPemohon;
    String NotelpPemohon;
    String Alasan;
    String AlmtygDituju;
    String RTdituju;
    String RWdituju;
    String KodePosdituju;
    String NoTelpdituju;
    String NamaOrtu;
    String AlmtOrtu;
    String RTOrtu;
    String RWOrtu;
    String KodePosOrtu;
    String NoTelpOrtu;
    String NamaPenjamin;
    String AlmtPenjamin;
    String RTPenjamin;
    String RWPenjamin;
    String KodePosPenjamin;
    String NoTelpPenjamin;
    String action, provinsi, kabupaten, kecamatan, desa;
    Kipmol bean = new Kipmol();
    ValidateForm validateform = new ValidateForm();
    DBClass dbobject = new DBClass();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //get the values from jsp page
            namaPemohon = request.getParameter("NamaPemohon");
            NIKPemohon = request.getParameter("NIKPemohon");
            NoKK = request.getParameter("NoKKPemohon");
            AlmtPemohon = request.getParameter("alamatAsal");
            RTPemohon = request.getParameter("RTpemohon");
            RWPemohon = request.getParameter("RWpemohon");
            KodePosPemohon = request.getParameter("kodepospemohon");
            NotelpPemohon = request.getParameter("noTeleponpemohon");
            Alasan = request.getParameter("alasan");
            AlmtygDituju = request.getParameter("alamattujuan");
            RTdituju = request.getParameter("RTtujuan");
            RWdituju = request.getParameter("RWtujuan");
            KodePosdituju = request.getParameter("kodepostujuan");
            NoTelpdituju = request.getParameter("noTelepontujuan");
            NamaOrtu = request.getParameter("namaOrangTua");
            AlmtOrtu = request.getParameter("AlamatOrangTua");
            RTOrtu = request.getParameter("RTortu");
            RWOrtu = request.getParameter("RWortu");
            KodePosOrtu = request.getParameter("kodeposortu");
            NoTelpOrtu = request.getParameter("noTeleponortu");
            NamaPenjamin = request.getParameter("namaPenjamin");
            AlmtPenjamin = request.getParameter("AlamatPenjamin");
            RTPenjamin = request.getParameter("RTpenjamin");
            RWPenjamin = request.getParameter("RWpenjamin");
            KodePosPenjamin = request.getParameter("kodepospenjamin");
            NoTelpPenjamin = request.getParameter("noTeleponpenjamin");
            action = request.getParameter("action");
            provinsi = request.getParameter("provinsi");
            kabupaten = request.getParameter("kabupaten");
            kecamatan = request.getParameter("kecamatan");
            desa = request.getParameter("desa");
            if (action.equals("submit")) {
                //set values to bean.For this call below method
                setValuesToBean();

                //check all form values are valid or not. send bean object 
                Kipmol checkedbean = validateform.validateData(bean);
                if (!checkedbean.getIsValid()) {

                    //if data is invalid.set bean object in request and pass that request to
                    //insertupdate.jsp using forward
                    checkedbean.setAction("submit");
                    request.setAttribute("error", checkedbean);

                    RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp");
                    rd.forward(request, response);
                    //now display errors in that jsp page
                } else {
                    //using DBClass object call insertDetails method and pass bean object
                    dbobject.insertDetails(bean);
                    List list = dbobject.getAlldetails();
                    request.setAttribute("list", list);
                    //forward to insertupdate page using requestdispatcher
                    RequestDispatcher rd = request.getRequestDispatcher("lihatdaftar.jsp");
                    //display a message to client.store message in request object
                    //forwarding to jsp
                    rd.forward(request, response);


                }
            }
//            if (action.equals("edit")) {
//                //get userdetails of particular name
//                Kipmol ubean = dbobject.getDetails(namaPemohon);
//                request.setAttribute("updateuser", ubean);
//                RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp");
//                rd.forward(request, response);
//            }
//            if (action.equals("update")) {
//
//
//                setValuesToBean();
//                Kipmol checkedbean = validateform.validateData(bean);
//                System.out.println(checkedbean.getIsValid());
//                if (!checkedbean.getIsValid()) {
//
//                    //if data is invalid.set bean object in request and pass that request to
//                    //insertupdate.jsp using forward
//                    checkedbean.setAction("update");
//                    request.setAttribute("error", checkedbean);
//                    RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp");
//                    rd.forward(request, response);
//                    //now display errors in that jsp page
//                } else {
//                    //using DBClass object call insertDetails method and pass bean object
//                    dbobject.UpateDetails(bean, namaPemohon);
//                    List list = dbobject.getAlldetails();
//                    request.setAttribute("list", list);
//                    //forward to insertupdate page using requestdispatcher
//                    RequestDispatcher rd = request.getRequestDispatcher("view.jsp");
//                    //display a message to client.store message in request object
//                    //forwarding to jsp
//                    rd.forward(request, response);
//
//
//                }
//
//            }
//            if (action.equals("delete")) {
//                //delete userdetails of particular name
//                dbobject.deleteDetails(namaPemohon);
//                List list = dbobject.getAlldetails();
//                request.setAttribute("list", list);
//                RequestDispatcher rd = request.getRequestDispatcher("view.jsp");
//                rd.forward(request, response);
//            }
        } catch (Exception e) {
            out.println(e);
        } finally {
            out.close();
        }
    }
    //this method is used to setvalues to bean

    public void setValuesToBean() {
        bean.setNamaPemohon(namaPemohon);
        bean.setNIKPemohon(NIKPemohon);
        bean.setNoKK(NoKK);
        bean.setAlmtPemohon(AlmtPemohon);
        bean.setRTPemohon(RTPemohon);
        bean.setRWPemohon(RWPemohon);
        bean.setKodePosPemohon(KodePosPemohon);
        bean.setNoTelpPemohon(NotelpPemohon);
        bean.setAlasan(Alasan);
        bean.setAlmtYgDituju(AlmtygDituju);
        bean.setRTDituju(RTdituju);
        bean.setRWDituju(RWdituju);
        bean.setKodePosDituju(KodePosdituju);
        bean.setNoTelpDituju(NoTelpdituju);
        bean.setNamaOrtu(NamaOrtu);
        bean.setAlmtOrtu(AlmtOrtu);
        bean.setRTOrtu(RTOrtu);
        bean.setRWOrtu(RWOrtu);
        bean.setKodePosOrtu(KodePosOrtu);
        bean.setNoTelpOrtu(NoTelpOrtu);
        bean.setNamaPenjamin(NamaPenjamin);
        bean.setAlmtPenjamin(AlmtPenjamin);
        bean.setRTPenjamin(RTPenjamin);
        bean.setRWPenjamin(RWPenjamin);
        bean.setKodePOsPenjamin(KodePosPenjamin);
        bean.setNoTelpPenjamin(NoTelpPenjamin);
        bean.setProvinsi(provinsi);
        bean.setKabupaten(kabupaten);
        bean.setKecamatan(kecamatan);
        bean.setDesa(desa);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    public String getServletInfo() {
        return "Short description";
    }
}
